package com.zhang.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.file.entity.Files;
import com.zhang.file.service.IFileService;
import com.zhang.member.dto.UserPostsSearchDTO;
import com.zhang.member.entity.UserPosts;
import com.zhang.member.entity.UserpostsFile;
import com.zhang.member.mapper.UserPostsMapper;
import com.zhang.member.service.IUserPostsService;
import com.zhang.member.service.UserpostsFileService;
import com.zhang.member.vo.UserPostListVO;
import com.zhang.member.vo.UserPostsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserPostsServiceImpl extends ServiceImpl<UserPostsMapper, UserPosts> implements IUserPostsService {
    @Autowired
    private IFileService fileService;
    @Autowired
    private UserpostsFileService userpostsFileService;

    @Override
    public void ModifyUserPosts(UserPosts userPosts) {
        updateById(userPosts);
    }

    @Override
    public List<UserPostListVO> getUserPostsList(UserPostsSearchDTO dto) {
        return baseMapper.getUserPostsList(dto);
    }

    @Override
    @Transactional
    public void createUserPosts(UserPosts userPosts) {
        userPosts.setCreateTime(LocalDateTime.now());
        List<String> fileIds = userPosts.getFileIds();
        save(userPosts);
        if (fileIds != null) {
            fileIds.forEach(item -> {
                Files byId = fileService.getById(item);
                if (byId!=null){
                    UserpostsFile userpostsFile = new UserpostsFile();
                    userpostsFile.setFileId(byId.getId().toString());
                    userpostsFile.setUserpostsId(userPosts.getId().toString());
                    userpostsFile.setType(byId.getFileType());
                    userpostsFileService.save(userpostsFile);
                }
            });
        }
    }

    @Override
    public UserPostsInfoVO getUserPostsById(String userPostsId) {
        baseMapper.AddShowCount(userPostsId);
        return baseMapper.getUserPostsInfoVOById(userPostsId);
    }

}
